<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 公共接口
 */
class Merch extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    /**
     * 店铺信息
     */

    public function getmymerch()
    {


        $merchinfo = Db::name('merch')
            ->where('user_id',$this->auth->id)
            ->find();

        $merch=0;
        if ($merchinfo&&$merchinfo['status']==1){
            $merch=1;
        }else if ($merchinfo&&$merchinfo['status']==0){
            $merch=2;
        }else{
            $merch=0;
        }

        $this->success('店铺信息',['merch'=>$merch  ]);


    }


    /**
     * 订单数据
     */
    public function getorderinfo()
    {
        $uid=$this->auth->id;


        $count=Db::name('orderlist')
            ->where('saleuser',$uid)
            ->where('status','gt',0)
            ->count();

        $money=Db::name('orderlist')
            ->where('saleuser',$uid)
            ->where('status','gt',0)
            ->sum('money');

        $getmoney=Db::name('orderlist')
            ->where('saleuser',$uid)
            ->where('status','gt',0)
            ->sum('getmoney');


        $this->success('订单数据',['count'=>$count,'money'=>$money,'getmoney'=>$getmoney]);

    }


    /**
     *  订单列表
     */
    public function getorderlist()
    {
        $uid=$this->auth->id;
        $status=$this->request->param('status',0);

        $list=Db::name('orderlist')
            ->alias('a')
            ->join('user b','a.buyuser=b.id')
            ->join('goods c','a.goods_id=c.id')
            ->join('user d','a.saleuser=d.id')
            ->where('a.buyuser',$uid)
            ->where('a.status','eq',$status)
            ->order('id desc')
            ->field('c.image,a.*,b.nickname as buyusername,c.name as goodname,d.nickname as salename,b.mobile as buymobile,d.mobile as salemobile')
            ->select();


        foreach ($list as &$v){
            $v['image']=cdnurl($v['image'],true);
            $v['addtime']=date('Y-m-d H:i',$v['addtime']);

            $merchinfo=Db::name('merch')
                ->where('user_id',$v['saleuser'])
                ->find();

            $v['payimg']=$this->getimg($merchinfo);


        }

        $this->success('订单列表',$list);


    }

    /**
     *  转换图片
     * zfimage` varchar(255) DEFAULT NULL COMMENT '支付宝收款码',
     * `zfsjimage` varchar(255) DEFAULT NULL COMMENT '支付宝商家码',
     * `payimage` varchar(255) DEFAULT NULL COMMENT '微信收款码',
     */

    public function getimg($arr)
    {

       $array=[];

       if ($arr['zfimage']){
           $array[]=cdnurl($arr['zfimage'],true);
       }
       if ($arr['zfsjimage']){
           $array[]=cdnurl($arr['zfsjimage'],true);
       }
       if ($arr['payimage']){
           $array[]=cdnurl($arr['payimage'],true);
       }

       return $array;

    }

    /**
      *  修改订单
     *
     */
    public function setorerpayimg(){
        $id=$this->request->param('id',0);
        $payimg=$this->request->param('img','');

        $ret=Db::name('orderlist')
            ->where('id',$id)
            ->update(['payimage'=>$payimg,'status'=>1,'paytime'=>time()]);

        if ($ret){
            $this->success('修改成功');
        }
        $this->error('修改失败');


    }







}